<!DOCTYPE html>
<html>
<head>
    <title>登录页面</title>
    <meta charset="utf-8">
</head>
<script type="text/javascript">

    function selectUser(email) {
        var xmlHttp = new XMLHttpRequest();
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                document.getElementById("showMsg").innerHTML = xmlHttp.responseText;
            }
        }
        xmlHttp.open("POST", "user/showUser.do", true);
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlHttp.send("email=" + email);
    }


    /*
    ajax回调
    */
    function register(email, password) {

        var xmlHttp;
        if (window.XMLHttpRequest) {
            //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
            xmlHttp = new XMLHttpRequest();
        } else {
            // IE6, IE5 浏览器执行代码
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlHttp.onreadystatechange = function () {
            /*
           xmlhttp.readyState
           0: 请求未初始化
           1: 服务器连接已建立
           2: 请求已接收
           3: 请求处理中
           4: 请求已完成，且响应已就绪
           */
            if (xmlHttp.readyState == 4)//请求完成
            {
                if (xmlHttp.status == 200) {
                    alert("注册结果：" + xmlHttp.responseText);
                } else {
                    document.getElementById("showMsg").innerHTML = "注册失败：xmlhttp.status=" + xmlHttp.status;
                }
            }
        }
        xmlHttp.open("POST", "user/register.do", true);
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlHttp.send("email=" + email + "&" + "password=" + password);
    }

    /**
     * 登录
     * @param email
     * @param password
     */
    function login(email, password) {
        var xmlHttp = new XMLHttpRequest();
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                document.getElementById("showMsg").innerHTML = xmlHttp.responseText;
            }
        }
        xmlHttp.open("POST", "user/login.do", true);
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlHttp.send("email=" + email + "&" + "password=" + password);

    }
</script>

<body>

<div id="z1">
    <input id="z1_input" type="text" placeholder="请输入您的邮箱"/>
</div>

<!-- Unnamed (文本框) -->
<div id="z2">
    <input id="z2_input" type="text" placeholder="请输入您的密码"/>
</div>

<button type="button" onclick="selectUser(z1_input.value)">查询</button>
<button type="button" onclick="login(z1_input.value,z2_input.value)">登录</button>
<button type="button" onclick="register(z1_input.value,z2_input.value)">注册</button>
<p id="showMsg">信息</p>
</body>
</html>
